package com.ambitor.ipro.actions;

import java.sql.SQLException;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.ambitor.ipro.app.gen.TaskGeneratorHelper;
import com.ambitor.ipro.entities.Result;
import com.ambitor.ipro.entities.TradeState;
import com.ambitor.ipro.entities.TradeType;
import com.ambitor.ipro.util.Constants;
import com.ambitor.ipro.util.ResultCode;
import com.ambitor.ipro.util.SkyenccUtil;
import com.ambitor.ipro.view.Pager;

public class ModifyIproAction extends JsonBaseAction {

	private static final long serialVersionUID = 1L;
	private transient SuperDao dao = null;
	private Result result = new Result(-1, "");

	public ModifyIproAction() throws SQLException {
		dao = new SuperDaoImpl(pd.getConn());
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	@Override
	public Pager getPager() {
		return pager;
	}

	@Override
	public IDataset getDataset() {
		return dataset;
	}

	@SuppressWarnings("unchecked")
	public String modifyNow() throws Exception {
		String proIds = pd.getRequestParameter("PRO_IDS");
		String frequencys = pd.getRequestParameter("FREQUENCYS");
		String userId = pd.getTopUserId();
		String nick = pd.getTopNick();
		if (StringUtils.isNotBlank(proIds)
				&& StringUtils.isNotBlank(frequencys)) {
			String[] proIdArray = proIds.split(",");
			String[] frequencyArray = frequencys.split(",");
			IDataset updateUserProBatchList = new DatasetList();
			IDataset tradeProBatchList = new DatasetList();
			for (int i = 0; i < proIdArray.length; i++) {
				String nextExecTime = SkyenccUtil.getNextInstallDate(
						new Date(), Integer.parseInt(frequencyArray[i]));
				IData updateUserProBatch = new DataMap();
				updateUserProBatch.put("USER_ID", userId);
				updateUserProBatch.put("NICK", nick);
				updateUserProBatch.put("NEXT_EXEC_TIME", nextExecTime);
				updateUserProBatch.put("PRO_ID", proIdArray[i]);

				updateUserProBatchList.add(updateUserProBatch);
				IData tradeProBatch = buildTrdeProBatch(updateUserProBatch);
				tradeProBatchList.add(tradeProBatch);
			}
			try {
				dao.update("TF_P_USER_PRO_BATCH", updateUserProBatchList);
				TaskGeneratorHelper.addTask(dao, tradeProBatchList);
			} catch (Exception e) {
				logger.warn("生成任务的过程中出现异常", e);
			}
			result.setCode(ResultCode.SUCCESS);
		}

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private IData buildTrdeProBatch(IData userProBatch) throws Exception {
		IData tradeProBatch = new DataMap();
		tradeProBatch.put("USER_ID", userProBatch.getString("USER_ID"));
		tradeProBatch.put("NICK", userProBatch.getString("NICK"));
		tradeProBatch.put("TRADE_STATE", TradeState.UN_EXECUTE);// 未执行
		tradeProBatch.put("TRADE_TYPE", TradeType.NOW_INSTALL);// 扫描安装
		tradeProBatch.put("PRO_ID", userProBatch.getString("PRO_ID"));
		tradeProBatch.put("PRIORITY", Constants.LOW_PRIORITY);
		tradeProBatch.put("CREATE_TIME", SkyenccUtil.formatDate(new Date()));
		tradeProBatch.put("PLAN_EXEC_TIME", SkyenccUtil.formatDate(new Date()));
		tradeProBatch.put("REMARK", "立即更换");
		return tradeProBatch;
	}

}
